#ifndef LABEL_H
#define LABEL_H

#include <qlabel.h>
#include <qpixmap.h>

class UrlLabel
	: public QLabel
{
	Q_OBJECT

	bool _pressed;

public:
	UrlLabel(QWidget * parent = 0, const char * name = 0)
		: QLabel(parent, name)
		, _pressed(false)
	{
		setText(tr("vinnitu@gmail.com"));
		QFont font("Arial", 10);
		font.setUnderline(false);
		setFont(font);	
		setPaletteForegroundColor(Qt::blue);
		setBackgroundColor(parent->paletteBackgroundColor());
	}

	virtual ~UrlLabel()
	{
		qDebug("~UrlLabel");
	}

	void enterEvent(QEvent * e)
	{
		QFont f = font();
		f.setUnderline(true);
		setFont(f);
		QLabel::enterEvent(e);
	}

	void leaveEvent(QEvent * e)
	{
		QFont f = font();
		f.setUnderline(false);
		setFont(f);
		QLabel::leaveEvent(e);
	}	

	void mousePressEvent(QMouseEvent * e)
	{
		QLabel::mousePressEvent(e);
		_pressed = (e->button() == Qt::LeftButton);
	}

	void mouseReleaseEvent(QMouseEvent * e)
	{		
		if (_pressed && e->button() == Qt::LeftButton && hitLabel(e->pos()))
			emit clicked();

		_pressed = false;
	}

	bool hitLabel(const QPoint & p)
	{
		return rect().contains(p);
	}

signals:
	void clicked();
};

class Label : public QLabel
{
	Q_OBJECT

	QPixmap _pixmapNormal;
	QPixmap _pixmapOver;
	QPixmap _pixmapDown;
	QPixmap _pixmapDisabled;

	bool _pressed;
	bool _disabled;

public:
	Label(const char * fn, QWidget * parent, const char * name = 0)
		: QLabel(parent, name)
		, _pixmapNormal(QPixmap::fromMimeSource(QString("%1def.png").arg(fn)))
		, _pixmapOver(QPixmap::fromMimeSource(QString("%1hover.png").arg(fn)))
		, _pixmapDown(QPixmap::fromMimeSource(QString("%1down.png").arg(fn)))
		, _pixmapDisabled(QPixmap::fromMimeSource(QString("%1disable.png").arg(fn)))
		, _pressed(false)
		, _disabled(false)
	{
		setPixmap(_pixmapNormal);
		setAutoResize(true);
	}

	virtual ~Label() {};

	void setDisabled(bool b)
	{
		_disabled = b;
		setPixmap(_disabled ? _pixmapDisabled : _pixmapNormal);
	}

signals:
	void clicked();

protected:
	void enterEvent(QEvent * e)
	{
		if (_disabled)
			return;

		QLabel::enterEvent(e);
		setPixmap(_pressed ? _pixmapDown : _pixmapOver);
	}

	void leaveEvent(QEvent * e)
	{
		if (_disabled)
			return;

		QLabel::leaveEvent(e);

		setPixmap(_pixmapNormal);
	}

	void mousePressEvent(QMouseEvent * e)
	{
		if (_disabled)
			return;

		QLabel::mousePressEvent(e);

		_pressed = (e->button() == Qt::LeftButton);
		setPixmap(_pressed ? _pixmapDown : _pixmapOver);
	}

	void mouseReleaseEvent(QMouseEvent * e)
	{
		if (_disabled)
			return;

		if (_pressed && e->button() == Qt::LeftButton && hitLabel(e->pos()))
			emit clicked();

		_pressed = false;
		setPixmap(_pressed ? _pixmapDown : _pixmapOver);
	}

	bool hitLabel(const QPoint & p)
	{
		return rect().contains(p);
	}
};

#endif //LABEL_H
